<?php 
class ListController extends CommonController{
	
	public function index(){
		
		//nav循环
		$nav=$this->nav();
		$this->assign('nav',$nav);
		
		//左侧导航循环
		$Father=$this->cate();
		$this->assign('cate',$Father);
		
		
		
		//从地址栏获得pid 找出pid等于得到的pid 的分类
		$pid=Q("get.pid",'',"intval");
		$cid=Q("get.cid","","intval");
		$cate=K("Category")->where("pid={$pid}")->all();
//		p($cate);
		$this->assign('cateall',$cate);

		//筛选条件  属性规格
		$gids = $this->cidGetGid($cid);
		//如果分类下面有商品
		if($gids){
			//通过gid查到商品属性(筛选的条件)
			$attr = K('Goodsattr')->gidGetAttr($gids);
			$this->assign('attr',$attr);
//			p($attr);
			
			//筛选参数
			$total = count($attr);
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $total, 0);
			//防止用户乱改param参数
			if(count($param) != $total) $param = array_fill(0, $total, 0);
			$this->assign('param',$param);
			
			//通过得到的$param进行筛选
			$lastGids = K('Goodsattr')->filterGids($param,$gids);
			
		}else{
			//如果分类没有商品
			$lastGids = array();
		}
//     p($lastGids);


		//查询商品表 里面的图片内容等等******************
		$good=K("Category")->all();
		$cid=Q("get.cid","","intval");
		$all=$this->son($good, $cid);
		$all[]=K("Category")->where("cid={$cid}")->getField("cid");
		
		$cids=implode(',', $all);
		$goods=array();
		foreach ($lastGids as $k => $v) {
			$goods[]=K("Goods")->where("category_cid in({$cids}) and gid={$v}")->find();
		}
		
				
		$this->assign('goods',$goods);
//		p($goods);
		
		
		
		//面包屑方法************8
		
		$data=K('Category')->all();
		$fatherCate=$this->_getFatherCate($data, $cid);
		$fatherCate=array_reverse($fatherCate);
		$this->assign('fatherCate',$fatherCate);
//		p($fatherCate);

        //处理面包屑导航下面子分类的部分*****
	    $soncate=K("Category")->where("pid={$cid}")->all();
//		如果没有子集
        if(empty($soncate)){
        	$pid=K("Category")->where("cid={$cid}")->getField("pid");
			$soncate=K("Category")->where("pid={$pid}")->all();        	
        }
		$this->assign('soncate',$soncate);
	
		$this->display();
	}

       /**
		 * 通过cid获得所对应的所有的商品id
		 */
		public function cidGetGid($cid){
			$cids = $this->getSon($cid);
			//调取缓存
			$gids = S("{$cid}_gid");
			//如果缓存失效
			if(!$gids){
				$gids = K('Goods')->where("category_cid IN(" . implode(',', $cids) . ")")->getField('gid',true);
				//缓存5分钟
				S("{$cid}_gid",$gids,60 * 5);
			}
			return $gids;
		}





	    /**
		 * 获得所有的子集,包括当前的cid
		 */
		public function getSon($cid){
			$data =K("Category")->all();
			$cids = $this->son($data, $cid);
			$cids[] = $cid;
			return $cids;
		}




     
      /**
	   * 获得子集递归方法 不包括自己
	   */
 
     public function son($goods,$cid){
     	$temp=array();
     	foreach ($goods as $k => $v) {
     		
     		if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->son($goods, $v['cid']));
			}
     		
     	}
		return $temp;
      	
     }
	
	
	
	
	
}



 ?>